﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System;

namespace SynoManager.Communication
{
    public class ClientRequestResult
    {
        public string disable_err;
        public bool success = true;
        public ErrorInformation errno;
        public ErrorInformation errinfo;

        //protected object[] items;
        //protected int total;

        public bool IsSuccess
        {
            get { return success; }
        }

        internal void Test()
        {
            if (errno != null)
                throw new ResultErrorException(errno);

            if (errinfo != null)
                throw new ResultErrorException(errinfo);

            if (!success)
                throw new CommunicationException("Request failed");
        }
    }

    public class ResultErrorException : CommunicationException
    {
        private readonly ErrorInformation information;

        public ResultErrorException(ErrorInformation information)
            :base("Station returned " + information.key)
        {
            this.information = information;
        }

        public string Key
        {
            get { return information.key; }
        }
    }

    public class ErrorInformation
    {
        public string key;
        public int line;
        public string section;
        public string sec;
    }

    public class LoginResult
    {
        public string Reason { get; set; }
        public string Result { get; set; }

        public string GetReasonMessage()
        {
            switch (Reason) 
            {
                case "error_guest":
                    return "Cannot login as guest. Please use another account to login.";
                case "error_cantlogin":
                    return "The account or password is invalid. Please try again.";
                case "error_expired":
                    return "Your account has been disabled. Please contact the administrator.";
                case "error_noprivilege":
                    return "You are not authorized to use this service.";
                case "error_systemfull":
                    return "You cannot login to the system because the disk space is full currently. Please restart the system and try again.";
                default:
                    return Reason;
            }
        }

        public void Test()
        {
            if (Result != "success")
            {
                throw new LoginException(Reason, GetReasonMessage());
            }
        }
    }
}